package GUI;

import javax.swing.JPanel;

import logic.Activity;
import logic.Book;
import logic.Loan;
import logic.Member;
import logic.Message;
import logic.Order;

import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Font;
import java.awt.Color;
import javax.swing.UIManager;

public class ResultDisplay extends JPanel {

	public final static int MEMBER = 1;
	public final static int BOOK = 2;
	public final static int LOAN = 3;
	public final static int ORDER = 4;
	public final static int REPORT = 5;
	
	private Client_GUI parent;
	private ArrayList<String> indexes;
	private ArrayList<Object> data;
	
	private ResultDisplay self;
	
	private JList list;
	int selection;
	
	
	//TODO set as public with getter
	public JPanel next;

	public ResultDisplay(final Client_GUI parent,final Message result,final JPanel next) {
		self = this;
		this.parent = parent;
		this.setSize(Client_GUI.MAIN_WIDTH, Client_GUI.MAIN_HEIGHT);
		setLayout(null);
		this.setBackground(UIManager.getColor("inactiveCaption"));

		this.next = next;
		
		indexes = result.GetMessageIndexes();
		data = result.GetMessageData();
		
		JLabel title = new JLabel("Select A Result");
		title.setForeground(Color.RED);
		title.setFont(new Font("Dialog", Font.BOLD, 16));
		title.setBounds(308, 11, 191, 27);
		add(title);
		
		JPanel panel = new JPanel();
		panel.setBackground(UIManager.getColor("inactiveCaptionBorder"));
		panel.setBounds(188, 447, 431, 56);
		add(panel);
		panel.setLayout(null);
		
		
		JButton selectButton = new JButton("Select");
		selectButton.setFont(new Font("Dialog", Font.BOLD, 14));
		selectButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				//TODO handle empty results
				if(list.isSelectionEmpty()) return;
				
				switch(selection){
				case MEMBER:{
					ArrayList<Member> memberList = (ArrayList<Member>)data.get(1);
					Member selected = memberList.get(list.getSelectedIndex());
					
					parent.safeToClear = true;

					
					if (next == null){
						ViewMember memberDisplayer = new ViewMember(parent, selected,self);
						parent.RepaintMain(memberDisplayer);

					}else{
						switch((Integer)data.get(2)){
						case SearchMember.SEARCH_FOR_CREATE_LOAN:{}
						case SearchMember.SEARCH_FOR_CREATE_ORDER:{
							((Book_opp)next).SetUid(selected);
							break;
						}
						
						case SearchMember.SEARCH_FOR_SEARCH_LOAN:{
							((SearchLoan)next).SetUID(String.valueOf(selected.getUID()));
							break;
						}
						
						case SearchMember.SEARCH_FOR_SEARCH_ORDER:{
							((SearchOrder)next).SetUID(String.valueOf(selected.getUID()));
							break;
						}
						case SearchMember.SEARCH_FOR_RETURN_LOAN:{
							((Return_Book)next).SetUID(String.valueOf(selected.getUID()));
							break;
						}
						
						}
						parent.RepaintMain(next);

					}
					break;
				}
				
				case BOOK:{
					ArrayList<Book> bookList = (ArrayList<Book>)data.get(1);
					Book selected = bookList.get(list.getSelectedIndex());
					
					parent.safeToClear = true;

					if (next == null){
						ViewBook bookDisplayer = new ViewBook(parent, selected,self);
						parent.RepaintMain(bookDisplayer);

					}else{
						switch((Integer)data.get(2)){
						case SearchBook.SEARCH_FOR_DELETE:{}
						case SearchBook.SEARCH_FOR_UPDATE:{
							((Manage_Stock)next).SetBID(selected);
							break;
						}
						
						case SearchBook.SEARCH_FOR_CREATE_LOAN:{}
						case SearchBook.SEARCH_FOR_CREATE_ORDER:{
							((Book_opp)next).SetBid(selected);
							break;
						}
						
						case SearchBook.SEARCH_FOR_SEARCH_LOAN:{
							((SearchLoan)next).SetBID(String.valueOf(selected.getBID()));
							break;
						}
						case SearchBook.SEARCH_FOR_SEARCH_ORDER:{
							((SearchOrder)next).SetBID(String.valueOf(selected.getBID()));
							break;

						}
						
						case SearchBook.SEARCH_FOR_LATE_RETURN:{
							((LateReturnReport)next).SetBID(String.valueOf(selected.getBID()));
							break;
						}
						
						}
						parent.RepaintMain(next);
					}
					
					break;
				}
				
				case LOAN:{
					ArrayList<Loan> loanList = (ArrayList<Loan>)data.get(1);
					Loan selected = loanList.get(list.getSelectedIndex());
					
					parent.safeToClear = true;

					if(next == null){
						ViewLoan loanDisplayer = new ViewLoan(parent, selected, self);
						parent.RepaintMain(loanDisplayer);

					}else{
						switch((Integer)data.get(2)){
						case SearchLoan.SEARCH_FOR_RETURN:{
							((Return_Book)next).SetLoan(selected.getBID(),selected.getBCID());
							break;
						}
						}
						parent.RepaintMain(next);
					}

					break;
				}
				
				case ORDER:{
					ArrayList<Order> orderList = (ArrayList<Order>)data.get(1);
					Order selected = orderList.get(list.getSelectedIndex());
					
					ViewOrder orderDisplayer = new ViewOrder(parent, selected, self);
					parent.safeToClear = true;
					parent.RepaintMain(orderDisplayer);
					
					break;
				}
				
				case REPORT:{
					ArrayList<Activity> actList = (ArrayList<Activity>)data.get(1);
					Activity selected = actList.get(list.getSelectedIndex());
					
					Activity_Report act = new Activity_Report(parent, selected, self);
					parent.safeToClear = true;
					parent.RepaintMain(act);
				}
				
				}
				
			}
		});
		selectButton.setBounds(10, 11, 151, 34);
		panel.add(selectButton);
		
		JButton cancelButton = new JButton("Cancel");
		cancelButton.setFont(new Font("Dialog", Font.BOLD, 14));
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				Home home = new Home(parent,parent.client.publicMessages);
				parent.RepaintMain(home);
			}
		});
		cancelButton.setBounds(270, 11, 151, 34);
		panel.add(cancelButton);
		
		//TODO fix the safeToClear for this class!!
		
		selection = (Integer)data.get(0);
		
		list = new JList();
		list.setBackground(UIManager.getColor("control"));
		list.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				parent.safeToClear = false;
			}
		});
		list.setBounds(45, 38, 718, 383);
		add(list);
		
		LoadResults();
	}
	
	
	/**
	 * This Function Handles the Results decoding appropriately to the content
	 */
	private void LoadResults(){
		
		ArrayList<String> listData = new ArrayList<String>();
		
		switch(selection){
		case MEMBER:{
			ArrayList<Member> memberList = (ArrayList<Member>)data.get(1);
			if (memberList == null) return;
			for (Member member : memberList){
				String toDisplay = member.getName() + " - " + member.getUID();
				listData.add(toDisplay);
				
			}
			break;
		}
		
		case BOOK:{
			ArrayList<Book> bookList = (ArrayList<Book>)data.get(1);
			if (bookList == null) return;
			for (Book book : bookList){
				String toDisplay = book.getname() + " - " + " - Edition: " + book.getedition();
				listData.add(toDisplay);
				
			}

			break;
		}
		
		case LOAN:{
			ArrayList<Loan> loanList = (ArrayList<Loan>)data.get(1);
			if (loanList == null) return;
			for(Loan loan : loanList){
				String toDisplay = "LID: " + loan.getLID() + ", UID: " + loan.getUID() + ", BID: " + loan.getBID() + ", BCID: " + loan.getBCID();
				listData.add(toDisplay);
			}
			
			break;
		}
		
		case ORDER:{
			ArrayList<Order> orderList = (ArrayList<Order>)data.get(1);
			if (orderList == null) return;
			for(Order order : orderList){
				String toDisplay = "OID: " + order.getOID() + ", UID: " + order.getUID() + ", BID: " + order.getBID();
				listData.add(toDisplay);
			}
			break;
		}

		case REPORT:{
			//TODOOOOOOOOOOO
	
			ArrayList<Activity> actList = (ArrayList<Activity>)data.get(1);
			if (actList == null) return;
			for(Activity act : actList){
				String toDisplay = "BID: " + act.getAID() + " OID: " + act.getREPORT_DATE();
				listData.add(toDisplay);
			}
			break;
		}
		
		}
		
		list.setListData(listData.toArray());
	}
}
